<?php include "classes.inc" ?>
<?php include "constants.inc" ?>
<?php session_start(); ?>
<?php
    $name = trim($_POST['name']);
    $stamnr = trim($_POST['stamnr']);
    $club = trim($_POST['club']);
    $elo = trim($_POST['elo']);
    
    if ( !empty($name) && strnatcasecmp($name, '?') != 0 ) {
        
        $connect = mysql_connect(DB_SERVER, DB_USER, DB_PWD);
        mysql_select_db(DB_CATALOG);
        $result = mysql_query("SET AUTOCOMMIT=0", $connect);
        $result = mysql_query("BEGIN", $connect);
        
        if ( !empty($stamnr) && strnatcasecmp($stamnr, '?') != 0 ) {
            
            $query = sprintf("INSERT INTO subscription (tournament_id, name, stamnr, date_added) VALUES (3, '%s', '%s', CURRENT_TIMESTAMP)",
                mysql_real_escape_string($name),
                mysql_real_escape_string($stamnr));

            if ($result = mysql_query($query, $connect)) {
                $result = mysql_query("COMMIT", $connect);
            } else {
                echo "Inschrijving kon niet worden opgeslagen, contacteer de webmaster: ".mysql_error();
            }
            
        } else if ( !empty($club) && strnatcasecmp($club, '?') != 0 && !empty($elo) && strnatcasecmp($elo, '?') != 0 ) {
            
            $query = sprintf("INSERT INTO subscription (tournament_id, name, club, elo, date_added) VALUES (3, '%s', '%s', '%s', CURRENT_TIMESTAMP)",
                mysql_real_escape_string($name),
                mysql_real_escape_string($club),
                mysql_real_escape_string($elo),
                mysql_real_escape_string($stamnr));
            
            if ($result = mysql_query($query, $connect)) {
                $result = mysql_query("COMMIT", $connect);
            } else {
                echo "Inschrijving kon niet worden opgeslagen, contacteer de webmaster: ".mysql_error();
            }
            
        } else {
            echo "Je moet ofwel je stamnummer ofwel je elo/clubnaam opgeven.";
        }       
        
    } else {
        echo "Piet Pompernikkel ? Je inschrijven zonder naam ?";
    }
?>
